#include "WinAppHelper.h"

#include "Windows.h"
#include "tchar.h"

WinAppHelper::WinAppHelper()
{

}

WinAppHelper &WinAppHelper::get()
{
    static WinAppHelper instance;
    return instance;
}

void WinAppHelper::set_startup(const QString &path,const QString &appid,bool isStartUp)
{
    HKEY hKey = NULL;
    LONG lReg = RegOpenKey(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run"), &hKey);
    if (lReg != ERROR_SUCCESS)
    {
        return;
    }

    do {
        if (isStartUp)
        {
            std::wstring filePath = path.toStdWString();
            int32_t dwRet = filePath.length() * 2;
            RegSetValueEx(hKey, appid.toStdWString().c_str(), 0, REG_SZ, (BYTE *)filePath.c_str(), dwRet);
        }
        else
        {
            RegDeleteValue(hKey, appid.toStdWString().c_str());
        }
    } while (0);

    if (hKey != NULL)
    {
        RegCloseKey(hKey);
    }
}
